package cn.house.rent.controller;

import cn.house.rent.entity.User;
import cn.house.rent.service.HouseService;
import cn.house.rent.utils.Result;
import cn.house.rent.vo.HouseVo;
import cn.house.rent.vo.TenantHomeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.UUID;

@RestController
@RequestMapping("house")
public class HouseController {
    @Autowired
    private HouseService houseService;

    //我的家模块查询和搜索查询接口
    @RequestMapping("tenanthome")
    public Object tenantHome(HouseVo houseVo, HttpSession session){
        User user = (User)session.getAttribute("user");
        Result result = houseService.tenantHome(user,houseVo);
        return result;
    }

    //租客  我的家  退租功能
    @RequestMapping("surrendertenant")
    private Object surrTenant(TenantHomeVo tenantHomeVo){
        Result result = houseService.surrTenant(tenantHomeVo);
        return result;
    }

    //租客  我的家   删除功能
    @RequestMapping("deltenhome")
    public Object delTenantHome(TenantHomeVo tenantHomeVo){
        Result result = houseService.delTenantHome(tenantHomeVo);
        return result;
    }

    //房东  房子管理  退租功能
    @RequestMapping("surrenderland")
    public Object surrLand(TenantHomeVo tenantHomeVo){
        Result result = houseService.surrLand(tenantHomeVo);
        return result;
    }

    //房东  房子管理  删除功能
    @RequestMapping("delland")
    public Object delLandHome(TenantHomeVo tenantHomeVo){
        Result result = houseService.delLandHome(tenantHomeVo);
        return result;
    }

    //房东  房子管理  同意退租功能
    @RequestMapping("passhome")
    public Object passHome(TenantHomeVo tenantHomeVo){
        Result result = houseService.passHome(tenantHomeVo);
        return result;
    }

    //房东  房子管理  驳回退租功能
    @RequestMapping("rejecthome")
    public Object rejectHome(TenantHomeVo tenantHomeVo){
        Result result = houseService.rejectHome(tenantHomeVo);
        return result;
    }

    //发布房源  房屋图片上传
    @RequestMapping("houseimg")
    public Object houseImg(MultipartFile file){
        String imgUrl = "F:\\project\\ideaprojects\\houseRentSys\\src\\main\\webapp\\static\\images\\houseimgs";
        //文件上传
        if (!file.isEmpty()){
            //获取上传文件的真实名字
            String originalFilename = file.getOriginalFilename();
            String filenameExtension = StringUtils.getFilenameExtension(originalFilename);
            String uuid = UUID.randomUUID().toString().replace("-", "");
            String fileUploadName = uuid.concat("."+filenameExtension);
            File file1 = new File(imgUrl, fileUploadName);
            try {
                file.transferTo(file1);
                return Result.builder().code(0).msg(fileUploadName).build();
            } catch (IOException e) {
                e.printStackTrace();
                return Result.builder().code(500).msg("上传失败！").build();
            }
        }else {
            return Result.builder().code(500).msg("未选择文件！").build();
        }
    }

    //发布房源
    @RequestMapping("addhouse")
    public Object addHouse(HttpSession session,HouseVo houseVo){
        User user = (User) session.getAttribute("user");
        Result result = houseService.addHouse(user,houseVo);
        return result;
    }

    //根据条件查房屋信息
    @RequestMapping("getHouseByInfo")
    public Object getHouseByInfo(HouseVo houseVo) {
        Result res = houseService.getHouseByInfo(houseVo);
        return res;
    }

    //通过id查房屋信息和房东信息
    @RequestMapping("getHouseAllById")
    public Object getHzHouseById(@RequestParam("houseId") Integer houseId) {
        Result houseAllById = houseService.getHouseAllById(houseId);
        return houseAllById;
    }

    //根据用户id 查询该用户所有发布的房源信息
    @RequestMapping("landHome")
    public Object getLandHome(HouseVo houseVo, HttpSession session) {
        User user = (User) session.getAttribute("user");
        Result result = houseService.selectByLandIdHome(user, houseVo);
        return result;
    }

    //房东  房子管理  编辑功能
    @RequestMapping("updatehouse")
    public Object updateHouse(HouseVo houseVo){
        Result result = houseService.updateHouse(houseVo);
        return result;
    }

}
